﻿import BaseClass from '@/utils/base_class';

export default class RepeatHandler extends BaseClass {
  private readonly handler: () => void;
  
  private state = 0;
  
  private time = performance.now();

  constructor(handler: () => void) {
    super();
    this.handler = handler;
  }

  public exec() {
    const time = performance.now();
    if (this.state === 0) {
      this.state = 1;
    } else if (this.state === 1) {
      if (time - this.time < 500) {
        return;
      }
      this.state = 2;
    } else {
      if (time - this.time < 50) {
        return;
      }
    }
    this.handler?.();
    this.time = time;
  }
  
  public reset() {
    this.state = 0;
  }
}
